package com.lagou.homework02;

public class ToStringTest {
    //定义一个转换方法
    public static String toStringTest(int[] arr){
        //如果传入数组为空那么返回字串 "null"
        if (arr == null){
            return "null";
        }
        //如果传入数组长度为空，那么返回"[]"
        int len = arr.length ;
        if (len == 0){
            return "[]";
        }
        //如果传入数组为{1,2,3,4,5}，那么返回"[1,2,3,4,5]"
        String str = "[";  //定义一个字符串  此时 str = "["
        for (int i = 0;i < len-1;i++){  //for循环拼接数组中的值和","
            str = str + arr[i] + ",";   //此时str = "[1,2,3,4,"
        }
        //数组最后一个值后面没有","，所以单独拼接
        str = str + arr[len-1] + "]";  //此时str = "[1,2,3,4,5]"
        return str; //返回字符串str
    }

}
